#include "item.h"
#include "data.h"


#include <QDebug>


Item::Item(Collection *parent) :
    AbstractData(parent)
{
}

Item::Item(const Item &other) :
    AbstractData(other)
{
    foreach (QByteArray prop, other.dynamicPropertyNames())
    {
        setProperty(prop, other.property(prop));
    }
}


QVariant Item::value(int field, int role) const
{
    switch (role)
    {
    case Qt::DisplayRole:
    {
        QString name = fieldName(field).toString();
        if (name.isEmpty()) return QVariant();

        return property(name.toAscii());
    }
    case Qt::EditRole:
    {
//        if (!optionReference(field).isEmpty()) return optionValues(field);

        return value(field, Qt::DisplayRole);
    }
    case EditTypeRole:
    {
        return editType(field);
    }

    default:
        break;
    }

    return QVariant();
}


bool Item::setValue(int field, const QVariant &value, int role)
{
    Q_UNUSED(role)

    QString name = fieldName(field).toString();
    if (name.isEmpty()) return false;

    if (!value.canConvert(QVariant::Map))
    {
        setProperty(name.toAscii(), value);
        emit sigChanged();
        return true;
    }

    QVariantMap valueMap = value.toMap();
    QStringList valueList;
    foreach (QString key, valueMap.keys())
    {
        if (!valueMap.value(key).toBool())
        {
            continue;
        }

        valueList << key;
    }

    setProperty(name.toAscii(), valueList.join(";"));
    emit sigChanged();
    return true;
}

QVariantMap Item::values() const
{
    MetaDataPtr meta = metaData();
    if (meta.isNull()) return QVariantMap();

    QVariantMap ret;
    foreach (const MetaItemPtr& item, meta->items())
    {
        if (item.isNull()) continue;
        ret.insert(item->field(), property(item->field().toAscii()));
    }
    return ret;
}

void Item::setValues(const QVariantMap &values)
{
    MetaDataPtr meta = metaData();
    if (meta.isNull()) return;

    foreach (const MetaItemPtr& item, meta->items())
    {
        if (item.isNull() || !values.contains(item->field())) continue;

        QVariant value = values.value(item->field());
        if (value.isNull()) continue;

        setProperty(item->field().toAscii(), value);
    }
}

//QStringList Item::options(int field) const
//{
//    Collection* coll = qobject_cast<Collection*>(parent());
//    if (!coll) return QStringList();

//    return coll->options(field);
//}

MetaData* Item::metaData() const
{
    Collection* c = qobject_cast<Collection*>(parent());
    if (!c) return 0;

    return c->metaData();
}

QString Item::optionReference(int field) const
{
    MetaData* meta = metaData();
    if (!meta) return QString();

    MetaItem* metaItem = meta->item(field);
    if (!metaItem) return QString();

    return metaItem->optionRef();
}

int Item::editType(int field) const
{
    MetaData* meta = metaData();
    if (!meta) return MetaItem::EditType_NONE;

    MetaItem* metaItem = meta->item(field);
    if (!metaItem) return MetaItem::EditType_NONE;

    return metaItem->editType();
}

//QVariantMap Item::optionValues(int field) const
//{
//    if (optionReference(field).isEmpty())
//    {
//        return QVariantMap();
//    }

//    QVariantMap ret;
//    QStringList values = value(field).toString().split(";");
//    foreach (QString option, options(field))
//    {
//        bool selected = values.contains(option);
//        ret.insert(option, selected);
//    }

//    return ret;
//}


